package com.d09cnpm2.group8.map;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.d09cnpm2.group8.DetailScreen;
import com.d09cnpm2.group8.adapter.LocationAdapter;
import com.d09cnpm2.group8.dao.TravelDataSource;
import com.d09cnpm2.group8.model.TravelLocation;

public class ListLocationActivity extends Activity {

    private TravelDataSource dataSource;
    private ListView listView;
    private String TAG = "----------List location activity----------->";
    private SharedPreferences sharedPreferences;
    private LocationAdapter adapter;
    private ArrayList<TravelLocation> locations;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.listview_location);
	dataSource = new TravelDataSource(this);
	sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
	boolean isFirst = sharedPreferences.getBoolean("isFirst", true);
	Log.e(TAG, "is first " + isFirst);
	dataSource.open();

	if (isFirst) {
	    String json = readJsonFromAssets("locations.json");
	    if (json != null)
		dataSource.loadInitialData(json.trim());
	    SharedPreferences.Editor editor = sharedPreferences.edit();
	    editor.putBoolean("isFirst", false);
	    editor.commit();
	}
	listView = (ListView) findViewById(R.id.list);
	locations = (ArrayList<TravelLocation>) dataSource.getAllLocations();

	adapter = new LocationAdapter(this, locations);
	listView.setAdapter(adapter);
	listView.setOnItemClickListener(itemClickListener);
    }
    

//---------------listview item click listener---------------------------------
    private OnItemClickListener itemClickListener = new OnItemClickListener() {

	@Override
	public void onItemClick(AdapterView<?> a, View view, int position,
		long id) {
	    Intent intent = new Intent(ListLocationActivity.this,
		    DetailScreen.class);
	    TravelLocation obj = locations.get(position);
	    Bundle b = new Bundle();
	    b.putString("name", obj.name);
	    b.putString("desc", obj.descript);
	    b.putDouble("lat", obj.lat);
	    b.putDouble("lng", obj.lng);
	    intent.putExtras(b);
	    startActivity(intent);
	}

    };

    @Override
    protected void onPause() {
	dataSource.close();
	super.onPause();
    }

    @Override
    protected void onResume() {
	dataSource.open();
	super.onResume();
    }

    public String readJsonFromAssets(String filename) {
	Log.i(TAG, "reading from json file");
	String json = null;
	StringBuffer stringBuffer = new StringBuffer();
	InputStream is = null;
	try {
	    is = this.getAssets().open(filename);
	    byte[] buffer = new byte[1024];
	    while (is.available() > 0) {
		is.read(buffer);
		stringBuffer.append(new String(buffer, "UTF-8"));
	    }

	} catch (IOException e) {
	    e.printStackTrace();
	} finally {
	    try {
		is.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}
	json = new String(stringBuffer);
	return json;
    }

    public void onClick(View view) {
	ArrayAdapter<TravelLocation> adapter = (ArrayAdapter<TravelLocation>) listView
		.getAdapter();
	TravelLocation newLocation = null;
	addLocation(newLocation);
	// startActivityForResult(intent, requestCode)

	// TravelLocation l = null;
	// Random r = new Random();
	// switch (view.getId()) {
	// case R.id.btn_addLocation:
	// l = new TravelLocation(r.nextInt(10) + 10, 90 + r.nextInt(100));
	// l.type = "beaches";
	// Log.e(TAG, dataSource.addLocation(l) + " ");
	// adapter.add(l);
	// break;
	// case R.id.btn_delLocation:
	// break;
	// default:
	// break;
	// }
	adapter.notifyDataSetChanged();
    }

    private void addLocation(TravelLocation newLocation) {

    }
}
